﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mail.Model;
using System.Net.Mail;

namespace Core.Razor.Template
{
    abstract class ServiceRequestEmailGeneratorBase
    {
        private StringBuilder _buffer;

        public string From { get; set; }

        protected EmailCategory CategoryModel
        {
            get;
            private set;
        }

        public MailMessage GenerateMailMessage
           (EmailCategory model
           )
        {
            // Update the model reference
            CategoryModel = model;

            // Clear any existing buffered content
            _buffer = new StringBuilder();

            // Render the template to the buffer
            Execute();

            // Return a new mail message with the buffer contents
            return new MailMessage(From, model.Name)
            {
                Body = _buffer.ToString()
            };
        }

        public abstract void Execute();

        public void Write(object value)
        {
            WriteLiteral(value);
        }

        public void WriteLiteral(object value)
        {
            _buffer.Append(value);
        }
    }
}
